﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class ClientsGroupByCompany : ClassManager.SessionManagerPage
    {
        private void GetAllClientsGroupByCampanyID(int CompanyID)
        {
            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.ClientsGroupsByCompanies> ClientsGroupsByCompaniesEntites = GPSTrackingDBV2.Data.DataRepository.ClientsGroupsByCompaniesProvider.GetByCompanyId(CompanyID);
            GPSTrackingDBV2.Data.DataRepository.ClientsGroupsByCompaniesProvider.DeepLoad(ClientsGroupsByCompaniesEntites, true, GPSTrackingDBV2.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingDBV2.Entities.ClientsGroups));

            ClientsGroupGridLinks.DataSource = ClientsGroupsByCompaniesEntites;
            ClientsGroupGridLinks.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //uscCompanySearch.OnAcceptEvent += uscCompanySearch_OnAcceptEvent;
            uscClientsGroupSearch.OnAddClientsGroupsEvent += uscClientsGroupSearch_OnAddClientsGroupsEvent;
        }

        void uscCompanySearch_OnAcceptEvent(Telerik.Web.UI.GridDataItem row)
        {
            int CompanyID = Convert.ToInt32(row.Cells[3].Text);

            //Refresh Data
            GetAllClientsGroupByCampanyID(CompanyID);
        }

        void uscClientsGroupSearch_OnAddClientsGroupsEvent(Telerik.Web.UI.GridDataItem row)
        {
            try
            {
                if (row != null)
                {
                    int CompanyID = -1;// uscCompanySearch.GetCompanyID();
                    if (CompanyID != -1)
                    {
                        //Insert
                        GPSTrackingDBV2.Entities.ClientsGroupsByCompanies ClientsGroupsByCompaniesEntity = new GPSTrackingDBV2.Entities.ClientsGroupsByCompanies();
                        ClientsGroupsByCompaniesEntity.CompanyId = CompanyID;
                        ClientsGroupsByCompaniesEntity.ClientGroupId = uscClientsGroupSearch.GetClientGroupID();

                        GPSTrackingDBV2.Data.DataRepository.ClientsGroupsByCompaniesProvider.Insert(ClientsGroupsByCompaniesEntity);

                        //Clear Controls
                        uscClientsGroupSearch.ClearControls();

                        //Refresh Data
                        GetAllClientsGroupByCampanyID(CompanyID);
                    }
                    else
                    {
                        RadWindowManager1.RadAlert("Debes seleccionar una compañia", 330, 180, "Error", null, null);
                    }
                }
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref ClientsGroupGridLinks);
            }
        }

        protected void ClientsGroupGridLinks_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            try
            {
                int CompanyID = -1;//uscCompanySearch.GetCompanyID();
                GPSTrackingDBV2.Entities.ClientsGroupsByCompanies ClientsGroupsByCompaniesEntity = ClassManager.InvokeEntityManager.Invoke_ClientsGroupsByCompanies_Entity(e);
                ClientsGroupsByCompaniesEntity.OriginalClientGroupId = ClientsGroupsByCompaniesEntity.ClientGroupId;
                ClientsGroupsByCompaniesEntity.OriginalCompanyId = ClientsGroupsByCompaniesEntity.CompanyId = CompanyID;
                GPSTrackingDBV2.Data.DataRepository.ClientsGroupsByCompaniesProvider.Delete(ClientsGroupsByCompaniesEntity);

                //Refresh Data
                GetAllClientsGroupByCampanyID(CompanyID);
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref ClientsGroupGridLinks);
            }
        }
    }
}